package com.runen.wnhz.runen.presenter.iPresenter;

import android.util.Log;

import com.runen.wnhz.runen.common.utils.ToastUtil;
import com.runen.wnhz.runen.data.entity.BaseEntity;
import com.runen.wnhz.runen.data.entity.CatalogListEntity;
import com.runen.wnhz.runen.data.entity.LessonIntroductionEntity;
import com.runen.wnhz.runen.data.entity.TestListEntity;
import com.runen.wnhz.runen.presenter.BasePresenter;
import com.runen.wnhz.runen.presenter.Contart.CourseDetailsContart;
import com.runen.wnhz.runen.presenter.model.CourseDetailsModel;
import com.runen.wnhz.runen.ui.fragment.major.IntroduceFragment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/5/5 0005.
 */

public class ICourseDetailsPresenter extends BasePresenter<CourseDetailsModel, CourseDetailsContart.View> {
    @Inject
    public ICourseDetailsPresenter(CourseDetailsModel mModel, CourseDetailsContart.View mView) {
        super(mModel, mView);
    }

    public void getLessonIntro(String token, String lid) {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("lid", lid);

        mModel.getLessonIntro(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity<LessonIntroductionEntity>>() {
                    @Override
                    public void call(BaseEntity<LessonIntroductionEntity> baseEntity) {
                        switch (baseEntity.getRe()) {
                            case -1:
                            case 0:
                                ToastUtil.showToast(baseEntity.getInfo());
                                break;
                            case 1:
                                mView.getLessonIntroRequest(baseEntity.getData());
                                break;
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.e(IntroduceFragment.TAG, throwable.getMessage());
                    }
                });

    }

    public void getLessonTest(String token, String lid, String page) {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("lid", lid);
        map.put("page", page);

        mModel.getLessonTest(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity<TestListEntity>>() {
                    @Override
                    public void call(BaseEntity<TestListEntity> baseEntity) {
                        switch (baseEntity.getRe()) {
                            case -1:
                            case 0:
                                ToastUtil.showToast(baseEntity.getInfo());
                                break;
                            case 1:
                                mView.getLessonTestRequest(baseEntity.getData());
                                break;
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.e("--->", throwable.getMessage());
                    }
                });
    }

    public void getCatalog(String token, String lid,String  page) {
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("lid", lid);
        map.put("page", page);

        mModel.getCatalog(map).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<BaseEntity<List<CatalogListEntity>>>() {
                    @Override
                    public void call(BaseEntity<List<CatalogListEntity>> listBaseEntity) {
                        switch (listBaseEntity.getRe()) {
                            case -1:
                            case 0:
                                ToastUtil.showToast(listBaseEntity.getInfo());
                                break;
                            case 1:
                                mView.getCatalogRequest(listBaseEntity.getData());
                                break;
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        Log.e("--->", throwable.getMessage());
                    }
                });
    }
}
